package server;

import com.formdev.flatlaf.FlatIntelliJLaf;
import database.TMSDatabase;
import gui.user.PassengerManageForm;
import info.Admin;
import info.Flight;
import info.Passenger;
import info.Ticket;
import messages.*;
import messages.AdminLoginRequestMessage;
import messages.AdminLoginResponseMessage;
import messages.admin.Admin_Infor_Ack_Message;
import messages.admin.Admin_Infor_Res_Message;
import messages.admin.UpAdmInfoReqMes;
import messages.admin.UpAdmInfoResponeMessage;
import messages.flight.*;
import messages.order.*;
import messages.passenger.Passenger_Infor_Ack_Message;
import messages.passenger.Passenger_Infor_Res_Message;
import messages.ticket.*;
import messages.user.*;
import messages.user.Flight_Query_Ack_Message;
import messages.user.Flight_Query_Res_Message;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ServerAdmin extends JFrame implements ActionListener {
    public static void main(String[] args) {
        FlatIntelliJLaf.setup();
        new ServerAdmin();
    }


    private JButton buttonEnable = new JButton("开启服务");
    private JButton buttonDisable = new JButton("关闭服务");

    private ServerSocket serverSocket = null;
    private int serverPort = 8888;

    public boolean enableService = false;

    public ServerAdmin() {
        this.setTitle("Server");
        this.setLocation(300, 200);
        this.setSize(600, 240);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridLayout(1, 2, 0, 0));

        buttonDisable.setEnabled(false);
        this.add(buttonEnable);
        this.add(buttonDisable);

        buttonEnable.addActionListener(this);
        buttonDisable.addActionListener(this);

        this.setVisible(true);
        enableService();
    }

    private void enableService() {
        // 等待启动服务
        while (!enableService) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 开放服务端口
        try {
            serverSocket = new ServerSocket(serverPort);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        // 使用while可以允许client端多次连接，但由于没有使用线程，同时只能有一个客户端连接服务器
        while (enableService) {
            // 监听端口请求，等待连接
            try {
                // 通过端口建立连接
                System.out.println("服务端启动成功，监听端口："+serverPort);
                Socket socket = serverSocket.accept();
                System.out.println("客户端连接成功："+socket.getInetAddress());
                handleClient(socket);
            } catch (SocketTimeoutException e) {
                // do nothing
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void sendMessage(Message msg, ObjectOutputStream oos) {
        try {
            oos.writeObject(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void closeClient(Socket socket) {
        if (socket != null) {
            try {
                socket.shutdownOutput();
                socket.shutdownInput();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class ClientHandler implements Runnable{
        private Socket socket;
        private ObjectInputStream ois;
        private ObjectOutputStream oos;
        public ClientHandler(Socket socket) {
            this.socket = socket;
            try {
                OutputStream os = socket.getOutputStream();
                InputStream is = socket.getInputStream();
                oos = new ObjectOutputStream(os);
                ois = new ObjectInputStream(is);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

        @Override
        public void run() {
            while (enableService) {
                Message msgReceived = null;
                try {
                    System.out.println("等待客户端请求。。。。");
                    msgReceived = (Message) ois.readObject();
                    System.out.println("收到客户端请求：" + msgReceived);
                } catch (ClassNotFoundException e) {
                    System.out.println(e.getMessage());
                } catch (IOException e) { // Socket 出错，停止对次 Socket 的处理
                    System.out.println(e.getMessage());
                    return;
                }

                switch (msgReceived.getMsg_type()) {
                    case MSG_LOGIN_REQ:
                        handleLoginRequest((LoginRequestMessage) msgReceived,oos);
                        break;
                    case MSG_REGISTER_REQ:
                        handleRegisterRequest((RegisterRequestMessage) msgReceived,oos);
                        break;
                    case MSG_UPINFO_REQ:
                        handleUPRequest((UPInfoRequestMessage) msgReceived,oos);
                        break;
                    case MSG_FLIGHT_QUERY_REQ:
                        handleFlightQueryRequest((Flight_Query_Res_Message) msgReceived,oos);
                        break;
                    case MSG_REFUND_REQ:
                        try {
                            handleRefundRequest((Flight_Refund_Res_Message) msgReceived,oos);
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case MSG_ORDER_INSERT_REQ:
                        handleInsertToOrderRequest((Order_Insert_Res_Message) msgReceived,oos);
                        break;
                    case MSG_ORDER_QUERY_REQ:
                        handleOrderQueryRequest((Order_Query_Res_Message) msgReceived,oos);
                        break;
                    case MSG_FLIGHT_PART_QUERY_REQ:
                        handleFlightTicketQueryRequest((Flight_Part_Query_Res_Message) msgReceived,oos);
                        break;
                    case MSG_ADMIN_LOGIN_REQ:
                        handleAdminLoginRequest((AdminLoginRequestMessage) msgReceived,oos);
                        break;
                    case MSG_UPADMINFO_REQ:
                        handleUpAdmInfoRequest((UpAdmInfoReqMes) msgReceived,oos);
                        break;
                    case MSG_SEARFLI_REQ:
                        handleSearFliRequest((SearFliRequestMessage) msgReceived,oos);
                        break;
                    case MSG_TICKET_QUERY_REQ:
                        handleTicketQueryRequest((Ticket_Query_Res_Message) msgReceived,oos);
                        break;
                    case MSG_USER_ORDER_QUERY_REQ:
                        handleUserOrderQueryRequest((UserOrder_Query_Res_Message) msgReceived,oos);
                        break;
                    case MSG_USER_ORDER_REFUND_REQ:
                        handleUserRedundRequest((UserOrder_Refund_Res_Message) msgReceived,oos);
                        break;
                    case MSG_PASSENGER_INFOR_QUERY_REQ:
                        handlePassengerInforQueryRequest((Passenger_Infor_Res_Message) msgReceived,oos);
                        break;
                    case MSG_ADMIN_INFOR_QUERY_REQ:
                        handleAdminInforQueryRequest((Admin_Infor_Res_Message) msgReceived,oos);
                        break;
                    case MSG_FLIGHT_QUERY_REQ1:
                        handleFlightQueryRequest1((Flight_Query_Res_Message1) msgReceived,oos);
                        break;
                    case MSG_FLIGHT_QUERY_REQ2:
                        handleFlightQueryRequest2((Flight_Query_Res_Message2) msgReceived,oos);
                        break;
                    case MSG_ADD_FLIGHTS_REQ:
                        handleFlightAddRequest((AddInfoRequestMessage) msgReceived,oos);
                        break;
                    case MSG_UPFLIINFO_REQ:
                        handleFlightUPRequest((UPFliInfoRequestMessage) msgReceived,oos);
                        break;
                    case MSG_DEFLIINFO_REQ:
                        handleDeFlightRequest((DeFliInfoRequestMessage) msgReceived,oos);
                        break;
                    case MSG_USER_ORDER_QUERY_REQ1:
                        handleUserOrderQueryRequest1((UserOrder_Query_Res_Message1) msgReceived,oos) ;
                        break;
                    case MSG_USER_ORDER_QUERY_REQ2:
                        handleUserOrderQueryRequest2((QueryOrderInfoRequestMessage) msgReceived,oos);
                        break;
                    case MSG_DE_ORDER_REQ:
                        handleDeOrderRequest((DeOrderInfoRequestMessage) msgReceived,oos);
                        break;
                    case MSG_TICKET_QUERY_REQ1:
                        handleQueryTicketRequest((Query_Ticket_Res_Message) msgReceived,oos);
                        break;
                    case MSG_ADD_FLIGHT_TICKET_REQ:
                        handleAddFlightTicketRequest((ADFlightsTicketReqInfoMes) msgReceived,oos);
                        break;
                    case MSG_TICKET_DELETE_REQ:
                        handleDeFlightTicketRequest((DeFlightsTicketsReqMsg) msgReceived,oos);
                        break;
                    case MSG_TICKET_UPDATE_REQ:
                        handleUpdateFlightTicketRequest((UpFliTicketsReqMsg) msgReceived,oos);
                        break;
                    case MSG_REF_USER_ORDER_QUERY_REQ:
                        handleQueryRefUserOrderRequest((Query_Ref_Order_Ticket_Res_Message) msgReceived,oos);
                        break;
                    case MSG_ADDRESS_QUERY_REQ:
                        handleFlightQueryByAddressRequest((Flight_Address_Query_Res_Message)msgReceived,oos);
                        break;
                    case MSG_QRESPONE_REQ:
                        handleQueryRequest((QRequestMessage) msgReceived,oos);
                        break;
                    default:
                        System.out.println("Uknown message received: "
                                + msgReceived.getMsg_type());
                        break;
                }
            }
            closeClient(socket);
        }
    }
    private void handleClient(Socket socket) {
        new Thread(new ClientHandler(socket)).start();
    }

    //处理用户登录消息
    private void handleLoginRequest(LoginRequestMessage login_req_msg,ObjectOutputStream oos){
        Passenger passenger = TMSDatabase.getInstance().passengerQuery(login_req_msg.getAccount());
        LoginResponseMessage login_ack_msg = new LoginResponseMessage();
        if(passenger != null){
            if(passenger.getPwd().equals(login_req_msg.getPwd())){
                System.out.println("密码匹配成功");
                login_ack_msg.setPassenger(passenger);
                login_ack_msg.setQueryStatus(LoginQueryStatus.SUCCESS);
            }else{
                System.out.println("密码匹配错误");
                login_ack_msg.setPassenger(null);
                login_ack_msg.setQueryStatus(LoginQueryStatus.PWD_ERROR);
            }
        }else{
            System.out.println("查无此人");
            login_ack_msg.setPassenger(null);
            login_ack_msg.setQueryStatus(LoginQueryStatus.NO_EXISIT);
        }
        sendMessage(login_ack_msg,oos);
    }

    //处理用户注册消息
    private void handleRegisterRequest(RegisterRequestMessage register_req_msg,ObjectOutputStream oos){
        Passenger passenger = register_req_msg.getPassenger();
        RegisterResponseMessage register_ack_msg = new RegisterResponseMessage();
        register_ack_msg.setSuccess(TMSDatabase.getInstance().insertPassenger(passenger));
        sendMessage(register_ack_msg,oos);
    }

    //处理用户修改个人信息消息
    private void handleUPRequest(UPInfoRequestMessage up_req_msg,ObjectOutputStream oos){
        UPInfoResponseMessage up_ack_msg = new UPInfoResponseMessage();
        Passenger passenger = TMSDatabase.getInstance().updatePassenger
                (up_req_msg.getPassenger(),up_req_msg.getOldIDNum());
        System.out.println(passenger);
        up_ack_msg.setNewPassenger(passenger);
        up_ack_msg.setSuccess(true);
        sendMessage(up_ack_msg,oos);
    }

    private void handleFlightTicketQueryRequest(Flight_Part_Query_Res_Message flight_part_query_res_message,ObjectOutputStream oos){
        Flight_Part_Query_Ack_Message flight_part_query_ack_message = new Flight_Part_Query_Ack_Message();
        flight_part_query_ack_message.setFlightTicketList(TMSDatabase.getInstance().partFlightsQuery(
                flight_part_query_res_message.getEDPA(),flight_part_query_res_message.getDEST()));
        sendMessage(flight_part_query_ack_message,oos);
    }

    //处理查询航班表消息
    private void handleFlightQueryRequest(Flight_Query_Res_Message flight_query_res_message,ObjectOutputStream oos){
        Flight_Query_Ack_Message flight_query_ack_message = new Flight_Query_Ack_Message();
        flight_query_ack_message.setFlightList(TMSDatabase.getInstance().flightsQuery());
        sendMessage(flight_query_ack_message,oos);
    }

    //处理退款消息
    private void handleRefundRequest(Flight_Refund_Res_Message flight_refund_res_message,ObjectOutputStream oos) throws ParseException {
        Flight_Refund_Ack_Message flight_refund_ack_message = new Flight_Refund_Ack_Message();
        if(flight_refund_res_message.getBtnText().equals("申请退款")){
                flight_refund_ack_message.setBtnText("已退款");
        }else if(flight_refund_res_message.getBtnText().equals("取消订单")){
            flight_refund_ack_message.setBtnText("已取消");
        } else if (flight_refund_res_message.getBtnText().equals("去付款")) {
            flight_refund_ack_message.setBtnText("已付款");
        }
        boolean isSuccess = TMSDatabase.getInstance().updateOrderStatus(flight_refund_res_message.getOrderNo(),
                flight_refund_res_message.getFlight(),
                flight_refund_res_message.getFclass(),
                flight_refund_res_message.getBtnText());
        flight_refund_ack_message.setSuccess(isSuccess);
        sendMessage(flight_refund_ack_message,oos);
    }

    private void handleInsertToOrderRequest(Order_Insert_Res_Message order_insert_res_message,ObjectOutputStream oos){
        Order_Insert_Ack_Message order_insert_ack_message = new Order_Insert_Ack_Message();
        TMSDatabase.getInstance().insertOrder(order_insert_res_message.getOrder());
        sendMessage(order_insert_ack_message,oos);
    }

    private void handleOrderQueryRequest(Order_Query_Res_Message order_query_res_message,ObjectOutputStream oos){
        Order_Query_Ack_Message order_query_ack_message = new Order_Query_Ack_Message();
        order_query_ack_message.setOrderList(TMSDatabase.getInstance().OrdersQuery(order_query_res_message.getIDNO()));
        sendMessage(order_query_ack_message,oos);
    }

    private void handleFlightQueryByAddressRequest(Flight_Address_Query_Res_Message flight_address_query_res_message,ObjectOutputStream oos){
        Flight_Address_Query_Ack_Message flight_address_query_ack_message = new Flight_Address_Query_Ack_Message();
        flight_address_query_ack_message.setFlightList(TMSDatabase.getInstance().flightListQueryByAddress(
                flight_address_query_res_message.getAddress()));
        sendMessage(flight_address_query_ack_message,oos);
    }
    private void handleQueryRequest(QRequestMessage qRequestMessage,ObjectOutputStream oos){
        QResponeMessage qResponeMessage=new QResponeMessage();
        qResponeMessage.setIsPrice(TMSDatabase.getInstance().queryPrice(qRequestMessage.getFlightid(),qRequestMessage.getStateType()));
        sendMessage(qResponeMessage,oos);
    }

























    //处理管理员登录消息
    private void handleAdminLoginRequest(AdminLoginRequestMessage admin_login_req_msg, ObjectOutputStream oos) {
        Admin admin = TMSDatabase.getInstance().adminQuery(admin_login_req_msg.getAdminId());
        AdminLoginResponseMessage admin_login_ack_msg = new AdminLoginResponseMessage();
        if (admin != null) {
            if (admin.getPwd().equals(admin_login_req_msg.getPwd())) {
                System.out.println("密码匹配成功");
                admin_login_ack_msg.setAdmin(admin);
                admin_login_ack_msg.setQueryStatus(LoginQueryStatus.SUCCESS);
            } else {
                System.out.println("密码匹配错误");
                admin_login_ack_msg.setAdmin(null);
                admin_login_ack_msg.setQueryStatus(LoginQueryStatus.PWD_ERROR);
            }
        } else {
            System.out.println("查无此人");
            admin_login_ack_msg.setAdmin(null);
            admin_login_ack_msg.setQueryStatus(LoginQueryStatus.NO_EXISIT);
        }
        sendMessage(admin_login_ack_msg, oos);
    }

    //管理员修改信息
    private void handleUpAdmInfoRequest(UpAdmInfoReqMes upAdmInfoReqMes, ObjectOutputStream oos) {
        UpAdmInfoResponeMessage up_ack_msg = new UpAdmInfoResponeMessage();
        Admin admin = TMSDatabase.getInstance().updateAdmin(upAdmInfoReqMes.getAdmin(),upAdmInfoReqMes.getOldIDNum());

        System.out.println(admin);
        up_ack_msg.setNewadmin(admin);
        up_ack_msg.setSuccess(true);
        sendMessage(up_ack_msg,oos);
    }

    /*管理员处理通过航班号查询航班信息*/
    public void handleSearFliRequest(SearFliRequestMessage searfli_req_msg, ObjectOutputStream oos) {
        String flightid = searfli_req_msg.getFlightID();
        List<Flight> flightList = null;

        if (flightList == null) {
//            flightList = new ArrayList<>(); // 确保 flightList 不为 null
            flightList= TMSDatabase.getInstance().flightsIdQuery(flightid);
        }
        SearFliResponseMessage searfli_ack_msg = new SearFliResponseMessage(flightList);
        if (!flightList.isEmpty()) {
            searfli_ack_msg.setSuccess(true);
            searfli_ack_msg.setFlightList(flightList); // 设置航班列表到响应消息中
        } else {
            searfli_ack_msg.setSuccess(false);
            searfli_ack_msg.setFlightList(new ArrayList<>()); // 返回空列表表示没有找到航班
        }
        sendMessage(searfli_ack_msg, oos); // 发送响应消息
    }

    /*查询票*/
    private void handleTicketQueryRequest(Ticket_Query_Res_Message ticket_query_res_message, ObjectOutputStream oos) {
        Ticket_Query_Ack_Message ticket_query_ack_message=new Ticket_Query_Ack_Message();
        ticket_query_ack_message.setTicketList(TMSDatabase.getInstance().ticketsQuery());
        sendMessage(ticket_query_ack_message,oos);
    }

    /*管理员查询用户订单信息*/
    private void handleUserOrderQueryRequest(UserOrder_Query_Res_Message userOrder_query_res_message, ObjectOutputStream oos) {
        UserOrder_Query_Ack_Message userOrder_query_ack_message=new UserOrder_Query_Ack_Message();
        userOrder_query_ack_message.setUserOrdersList(TMSDatabase.getInstance().userOrderQUery());
        sendMessage(userOrder_query_ack_message,oos);

    }


    /*管理员查询退票订单信息*/
    private void handleUserRedundRequest(UserOrder_Refund_Res_Message userOrder_refund_res_message, ObjectOutputStream oos) {
        UserOrder_Refund_Ack_Message userOrder_refund_ack_message=new UserOrder_Refund_Ack_Message();
        userOrder_refund_ack_message.setUserOrdersList(TMSDatabase.getInstance().userOrderRefund());
        sendMessage(userOrder_refund_ack_message,oos);
    }

    /*管理员查询用户信息*/
    private void handlePassengerInforQueryRequest(Passenger_Infor_Res_Message passenger_infor_res_message, ObjectOutputStream oos) {
        Passenger_Infor_Ack_Message passenger_infor_ack_message=new Passenger_Infor_Ack_Message();
        passenger_infor_ack_message.setPassengerList(TMSDatabase.getInstance().passengersInforQuery());
        sendMessage(passenger_infor_ack_message,oos);
    }

    /*管理员查询员工信息*/
    private void handleAdminInforQueryRequest(Admin_Infor_Res_Message admin_infor_res_message, ObjectOutputStream oos) {
        Admin_Infor_Ack_Message admin_infor_ack_message=new Admin_Infor_Ack_Message();
        admin_infor_ack_message.setAdminList(TMSDatabase.getInstance().adminInforQuery());
        sendMessage(admin_infor_ack_message,oos);
    }

    /*按航班号查询*/
    private void handleFlightQueryRequest1(Flight_Query_Res_Message1 flight_query_res_message1, ObjectOutputStream oos){
        Flight_Query_Ack_Message1 flight_query_ack_message1 = new Flight_Query_Ack_Message1();
        flight_query_ack_message1.setFlightList(TMSDatabase.getInstance().flightsIdQuery(flight_query_res_message1.getFlightId()));
//        FlightId.setFlightId(flight_query_res_message1.getFlightId());
//        ATM1 atm=new ATM1();
//        atm.setflightList(flight_query_ack_message1.getFlightList());
        sendMessage(flight_query_ack_message1,oos);
    }

    /*按地点查询*/
    private void handleFlightQueryRequest2(Flight_Query_Res_Message2 flight_query_res_message2, ObjectOutputStream oos){
        Flight_Query_Ack_Message2 flight_query_ack_message2 = new Flight_Query_Ack_Message2();
        flight_query_ack_message2.setFlightList(TMSDatabase.getInstance().flightsAreaQuery(flight_query_res_message2.getFlightArea()));
        sendMessage(flight_query_ack_message2,oos);
    }

    /*管理员增加航班信息*/
    private void handleFlightAddRequest(AddInfoRequestMessage addInfoRequestMessage, ObjectOutputStream oos) {
        AddInfoResponeMessage addInfoResponeMessage = new AddInfoResponeMessage();
        Flight flight = TMSDatabase.getInstance().AddFlights(addInfoRequestMessage.getFlight());
        System.out.println(flight);
        addInfoResponeMessage.setNewFlight(flight);
        addInfoResponeMessage.setSuccess(true);
        sendMessage(addInfoResponeMessage,oos);
    }

    /*管理员修改航班信息*/

    private void handleFlightUPRequest(UPFliInfoRequestMessage upFliInfoRequestMessage, ObjectOutputStream oos) {
        UPFLliInfoResponseMessage upfLliInfoResponseMessage = new UPFLliInfoResponseMessage();
        Flight flight =TMSDatabase.getInstance().updateFlight(upFliInfoRequestMessage.getFlight(),upFliInfoRequestMessage.getOldFliId());
        System.out.println(flight);
        upfLliInfoResponseMessage.setNewFlight(flight);
        upfLliInfoResponseMessage.setSuccess(true);
        sendMessage(upfLliInfoResponseMessage,oos);

    }

    private void handleDeFlightRequest(DeFliInfoRequestMessage deFliInfoRequestMessage, ObjectOutputStream oos) {
        DeFLliInfoResponseMessage deFLliInfoResponseMessage = new DeFLliInfoResponseMessage();
        deFLliInfoResponseMessage.setSuccess(TMSDatabase.getInstance().deleteFlight(deFliInfoRequestMessage.getOldFliId()));
        sendMessage(deFLliInfoResponseMessage,oos);
    }

    /*管理员查询订单信息*/
    private void handleUserOrderQueryRequest1(UserOrder_Query_Res_Message1 userOrder_query_res_message1, ObjectOutputStream oos) {
        UserOrder_Query_Ack_Message1 userOrder_query_ack_message1 = new UserOrder_Query_Ack_Message1();
        userOrder_query_ack_message1.setUserOrder(TMSDatabase.getInstance().userOrderQuery1(userOrder_query_res_message1.getUserOrder()));
        sendMessage(userOrder_query_ack_message1,oos);

    }

    /*管理员查看订单信息*/
    private void handleUserOrderQueryRequest2(QueryOrderInfoRequestMessage queryOrderInfoRequestMessage,ObjectOutputStream oos){
        QueryOrderInfoResponeMessage queryOrderInfoResponeMessage=new QueryOrderInfoResponeMessage();
        queryOrderInfoResponeMessage.setUserOrders(TMSDatabase.getInstance().userOrderQuery1(queryOrderInfoRequestMessage.getOldOrderId()));
        sendMessage(queryOrderInfoResponeMessage,oos);
    }

    private void handleDeOrderRequest(DeOrderInfoRequestMessage deOrderInfoRequestMessage,ObjectOutputStream oos){
        DeOrderInfoResponeMessage deOrderInfoResponeMessage = new DeOrderInfoResponeMessage();
        deOrderInfoResponeMessage.setSuccess(TMSDatabase.getInstance().deleteOrder(deOrderInfoRequestMessage.getOldUserId()));
        sendMessage(deOrderInfoResponeMessage,oos);
    }

    private void handleQueryTicketRequest(Query_Ticket_Res_Message query_ticket_res_message, ObjectOutputStream oos){
        Query_Ticket_Ack_Message query_ticket_ack_message = new Query_Ticket_Ack_Message();
        query_ticket_ack_message.setTicketList(TMSDatabase.getInstance().ticketsQuery1(query_ticket_res_message.getFclass()));
        sendMessage(query_ticket_ack_message,oos);
    }

    /*增加航班票种信息*/
    private void handleAddFlightTicketRequest(ADFlightsTicketReqInfoMes adFlightsTicketReqInfoMes, ObjectOutputStream oos) {
        ADFlightsTicketResInfoMes adFlightsTicketResInfoMes = new ADFlightsTicketResInfoMes();
        Ticket ticket =TMSDatabase.getInstance().AddFlightsTicket(adFlightsTicketReqInfoMes.getTicket());
        System.out.println(ticket);
        adFlightsTicketResInfoMes.setNewTicket(ticket);
        adFlightsTicketResInfoMes.setSuccess(true);
        sendMessage(adFlightsTicketResInfoMes,oos);

    }

    /*删除航班票种信息*/
    private void handleDeFlightTicketRequest(DeFlightsTicketsReqMsg deFlightsTicketsReqMsg,ObjectOutputStream oos){
        DeFlightsTicketsResMsg deFlightsTicketsResMsg = new DeFlightsTicketsResMsg();
        deFlightsTicketsResMsg.setSuccess(TMSDatabase.getInstance().DeleteFlightsTicket(deFlightsTicketsReqMsg.getOldFliId()));
        sendMessage(deFlightsTicketsResMsg,oos);
    }

    /*管理员修改费用*/
    private void handleUpdateFlightTicketRequest(UpFliTicketsReqMsg upFliTicketsReqMsg, ObjectOutputStream oos) {
        UpFliTicketsResMsg upFliTicketsResMsg = new UpFliTicketsResMsg();
        Ticket ticket = TMSDatabase.getInstance().updateFlightTicket(upFliTicketsReqMsg.getTicket(),upFliTicketsReqMsg.getOldFlightTicketId());
        System.out.println(ticket);
        upFliTicketsResMsg.setNewTicket(ticket);
        upFliTicketsResMsg.setSuccess(true);
        sendMessage(upFliTicketsResMsg,oos);
    }

    /*管理员查询已取消或已退款订单*/
    private void handleQueryRefUserOrderRequest(Query_Ref_Order_Ticket_Res_Message query_ref_order_ticket_res_message,ObjectOutputStream oos){
        Query_Ref_Order_Ticket_Ack_Message query_ref_order_ticket_ack_message=new Query_Ref_Order_Ticket_Ack_Message();
        query_ref_order_ticket_ack_message.setUserOrderList(TMSDatabase.getInstance().userOrderRefund1(query_ref_order_ticket_res_message.getOrderNo()));
        sendMessage(query_ref_order_ticket_ack_message,oos);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonEnable) {
            buttonEnable.setEnabled(false);
            buttonDisable.setEnabled(true);
            enableService = true;
        }else if (e.getSource() == buttonDisable) {
            // 系统退出服务，系统退出
            enableService = false;
            System.exit(0);
        }
    }
}
